package com.haobao.telephonerecord.utils

import android.content.Context
import androidx.appcompat.widget.AppCompatEditText
import android.text.TextWatcher
import android.text.Editable
import android.text.InputType
import android.text.TextUtils
import android.util.AttributeSet

/**
 * Created by ypk on 2022/3/2
 * 只能输入整数和小数的自定义编辑框，并支持可限制输入到小数点后几，默认只能输入小数点后2位
 */
class NumberEditTextView : AppCompatEditText {

    //限制的小数点后的位数
    var pointNumberDigits = 2

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    fun setPointNumber(number: Int) {
        pointNumberDigits = number;
    }

    /**
     * 支持是输入整数
     */
    fun setOnlySupportNumber() {
        inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED
    }

    /**
     * 支持输入整数和小数的
     */
    fun setSupportNumberDecimal() {
        inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
    }

    private fun init() {
        setSupportNumberDecimal()
        //校验输入框只能输入到小数点后 pointNumberDigits 位
        addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun afterTextChanged(editable: Editable) {
                val str = editable.toString()
                if (!TextUtils.isEmpty(str)) {
                    //校验输入的数字，开头只能输入一个零
                    if (str.startsWith("0")) {
                        if (str.length > 1) {
                            val secondChar = str.substring(1, 2)
                            println("NumberEditTextView.afterTextChanged secondChar=$secondChar")
                            if (secondChar != ".") {
                                if (secondChar == "0") {
                                    setText("0")
                                } else {
                                    setText(secondChar)
                                }
                                setSelection(1)
                                return
                            }

                        }
                    }

                    val pointIndex = str.indexOf(".")
                    if (pointIndex != -1) {
                        if (str.substring(pointIndex + 1, str.length).length > pointNumberDigits) {
                            val pointBeforeText = str.substring(0, pointIndex)
                            val content =
                                str.substring(pointIndex, pointIndex + 1 + pointNumberDigits)
                            val totalContent = "$pointBeforeText$content"
                            setText(totalContent)
                            setSelection(totalContent.length)
                        }
                    }


                }
            }
        })
    }
}